import { request } from '../../request/index.js';
const regeneratorRuntime = require('../../lib/runtime/runtime.js');
var time = require('../../utils/time');

// pages/order/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        value: '全部',
        id: 1,
        isActive: true,
      },
      {
        value: '待库款',
        id: 2,
        isActive: false,
      },
      {
        value: '待发货',
        id: 3,
        isActive: false,
      },
      {
        value: '退货/退款',
        id: 4,
        isActive: false,
      },
    ],
    type: 1, // tab类型
    orderList: [], // 订单列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let { type } = options;
    this.setData({
      type,
    });
    // 请求订单list需要token参数
    // 需要先判断
    const token = wx.getStorageSync('token');
    if (!token) {
      wx.navigateTo({
        url: '/pages/auth/index',
      });
      return;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 根据type重新渲染tabs
    let { type, tabs } = this.data;
    console.log('type', type);
    tabs.forEach((item, index) => {
      item.id == type ? item.isActive = true : item.isActive = false;
    });
    this.setData({
      tabs,
    });
    console.log(this.data.tabs);
    this.getorderList();
  },
  // Tabs切换
  handletabsItemChange(e) {
    console.log(e);
    // 获取index索引
    let { index } = e.detail;
    console.log(index);
    let { tabs, type } = this.data;
    tabs.forEach((item, index2) => {
      index2 === index ? item.isActive = true : item.isActive = false;
    });
    type = index + 1;
    this.setData({
      tabs,
      type,
    });
    this.getorderList();
  },
  // 获取订单list
  async getorderList() {
    let { type } = this.data;
    console.log('type', type);
    const res = await request({ url: '/my/orders/all', method: 'GET', data: { type: type } });
    console.log(res);
    let orderList = res.orders;
    orderList.forEach(item => {
      item.create_time_cn = time.formatTimeTwo(item.create_time, 'Y/M/D h:m:s');
    })
    this.setData({
      orderList,
    })
  }
})